package com.peng.utils;

import javax.swing.*;
import java.awt.*;

/**
 * @author : pengyf
 * @version : 1.0
 * @createTime : 2022/1/22 14:05
 * @Description : TODO
 */
public class FrameUtils {

    /**
     * @author pengyf
     * @date: 2022/1/22 11:17
     * @description:  设置窗体居中
     * 思路:
     *    A:获取屏幕的宽和高
     *    B:获取窗体的宽和高
     *    C:(用屏幕的宽-窗体的宽)/2,(用屏幕的高-窗体的高)/2作为窗体的新坐标
     */
    public static void setFrameCenter(JFrame jf) {
        //获取工具类
        Toolkit tk = Toolkit.getDefaultToolkit();
        //获取屏幕的宽和高
        Dimension d = tk.getScreenSize();
        double srceenWidth = d.getWidth();
        double srceenHeight = d.getHeight();
        //获取窗体的宽和高
        int frameWidth = jf.getWidth();
        int frameHeight = jf.getHeight();
        //获取新的宽和高
        int width = (int) (srceenWidth - frameWidth) / 2;
        int height = (int) (srceenHeight - frameHeight) / 2;
        //设置窗体居中
        jf.setLocation(width, height);
    }

    /**
     * @author pengyf
     * @date: 2022/1/22 11:17
     * @description:  设置弹窗居中
     * 思路:
     *    A:获取屏幕的宽和高
     *    B:获取窗体的宽和高
     *    C:(用屏幕的宽-窗体的宽)/2,(用屏幕的高-窗体的高)/2作为窗体的新坐标
     */
    public static void setDialogCenter(JDialog jd) {
        //获取工具类
        Toolkit tk = Toolkit.getDefaultToolkit();
        //获取屏幕的宽和高
        Dimension d = tk.getScreenSize();
        double srceenWidth = d.getWidth();
        double srceenHeight = d.getHeight();
        //获取窗体的宽和高
        int frameWidth = jd.getWidth();
        int frameHeight = jd.getHeight();
        //获取新的宽和高
        int width = (int) (srceenWidth - frameWidth) / 2;
        int height = (int) (srceenHeight - frameHeight) / 2;
        //设置窗体居中
        jd.setLocation(width, height);
    }
}
